<template>
    <section>
        <!--工具条-->

        <div class="section-content clearfix">
                <el-col :sm="24" :lg="24">
                        <div class="grid-content bg-purple" :style="{backgroundColor:titleColor}">
                            <div class="dev-title">{{editForm.mpName}}公众号信息</div>
                        </div>
                        <div class="col-bg clearfix">
                            <!-- <el-form  ref="editForm"  :model="editForm" label-width="100px" @submit.prevent="onSubmit" style="margin:20px;width:60%;float: left;">
                                <el-form-item label="appId" >
                                    <el-input v-model="editForm.appId" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="appSecret" >
                                    <el-input v-model="editForm.appSecret" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="templateId" >
                                    <el-input v-model="editForm.templateId" auto-complete="off"></el-input>
                                </el-form-item>
                           
                                <el-form-item>
                                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="editLoading">提交</el-button>
                                </el-form-item>
                            </el-form> -->
                            <!-- <el-form  v-if="initQCode" ref="editForm"  :model="editForm" label-width="100px" @submit.prevent="onSubmit" style="margin:20px;width:30%;float: left;">
                                <span style="margin-left: 10px">扫码接收微信告警</span>
                                <qriously  v-model="initQCode" :size="138"/>
                            </el-form> -->
                            <el-form   style="margin:20px;width:30%;float: left;">
                                    <div style="margin-left: 10px">扫码关注公众号</div>
                                    <img src="../../../assets/45ee4c7973fa6ca06f3cbd53c773e3a.png" width="138">
                                </el-form>
                            
                        </div>
                    </el-col>

            <!--列表-->
            
            <h2  style="font-size: 20px;font-weight: bold;color:#475669">公有云微信登录列表</h2>
            <el-table :data="query" highlight-current-row v-loading="listLoading" :header-row-style="{backgroundColor: titleColor}" @selection-change="selsChange" style="width: 100%;">
                
                <el-table-column type="index" label="序号"  width="80" :index="indexMethod">
                </el-table-column>
                <el-table-column prop="nickname" label="昵称" >
                </el-table-column>
                <!-- <el-table-column prop="openId" label="openId"  width="350">
                </el-table-column>
                <el-table-column prop="appId" label="appId"  width="200">
                </el-table-column> -->
                <el-table-column  label="地址">
                    
                        <template scope="scope">
                                {{scope.row.country}}
                                {{scope.row.province}}
                                {{scope.row.city}}
                            </template>
                </el-table-column>
               
                <el-table-column prop="createTime" label="创建时间"  width="200">
                </el-table-column> 
            
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                      <i @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete" circle title="删除" class="pb-icon icon-delete"></i>

                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                    style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" v-model="editFormVisible" :custom-class="dialogTitleColor" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="菜单中文名" prop="name">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单英文名" prop="name">
                        <el-input v-model="editForm.englishName" auto-complete="off"></el-input>
                    </el-form-item>

                    <!-- <el-form-item label="父菜单ID">
                            <el-radio-group v-model="addForm.sex">
                                <el-radio class="radio" :label="1">男</el-radio>
                                <el-radio class="radio" :label="0">女</el-radio>
                            </el-radio-group>
                        </el-form-item> -->
                    <el-form-item label="父菜单ID">
                        <el-input-number v-model="editForm.parentId" :min="0"></el-input-number>
                    </el-form-item>
                    <el-form-item label="优先级">
                        <el-input-number v-model="editForm.priority" :min="0"></el-input-number>
                    </el-form-item>
                    <el-form-item label="icon">
                        <el-input v-model="editForm.icon" :min="0"></el-input>
                    </el-form-item>
                    <el-form-item label="path" prop="path"  class="grade">
                        <el-input type="text" v-model="editForm.path"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="editLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" :custom-class="dialogTitleColor" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="菜单中文名" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off" class="grade"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单英文名" prop="name">
                        <el-input v-model="addForm.englishName" auto-complete="off" class="grade"></el-input>
                    </el-form-item>

                    <!-- <el-form-item label="父菜单ID">
					<el-radio-group v-model="addForm.sex">
						<el-radio class="radio" :label="1">男</el-radio>
						<el-radio class="radio" :label="0">女</el-radio>
					</el-radio-group>
				</el-form-item> -->
                    <el-form-item label="父菜单ID">
                        <el-input-number v-model="addForm.parentId" :min="0"></el-input-number>
                    </el-form-item>
                    <el-form-item label="优先级">
                        <el-input-number v-model="addForm.priority" :min="0"></el-input-number>
                    </el-form-item>
                    <el-form-item label="icon">
                        <el-input v-model="addForm.icon" :min="0"></el-input>
                    </el-form-item>
                    <el-form-item label="path" prop="path">
                        <el-input type="text" v-model="addForm.path" class="grade"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: center;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        company_weixin_post,
        company_weixin_put,
        company_weixin_my,
        user_weixin_query,
        user_weixin_delete
    } from './../../../api/api';

    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                initQCode:'',
                companyId:'',
                query: [],
                total:1,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列
                mptype:0,
                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    }]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                    type: '',
                    name: '',
                    englishName: '',
                    parentId: '',
                    path: '',
                    icon: '',
                    status: '',
                    priority: '',
                    createTime: ''
                },

                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    }],
                    path: [{
                            required: true,
                            message: '请输入path',
                            trigger: 'blur'
                        }

                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    englishName: '',
                    parentId: '',
                    path: '',
                    icon:''
                }

            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取菜单列表
            getUsers() {
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    
                    // name: this.filters.name
                };
                this.listLoading = true;
                //NProgress.start();
                user_weixin_query(para).then((res) => {
                    
                    this.listLoading = false;
                    if(res.code==0){
                    this.total = res.data.totalCount;
                    this.query = res.data.list;
                    }
                  
                    //NProgress.done();
                });
                company_weixin_my().then((res) => {
                    if(res.code==0){
                        if(res.data){
                        this.editForm=res.data;
                            this.mptype=0;
                            console.log(this.initQCode);
                            // this.initQCode='888888888888888';
                            console.log(this.companyId)
                            
                        this.companyId=res.data.companyId;  
                        this.initQCode='/api/auth/wechat/bind_alarm?appId='+res.data.appId+'&companyId='+this.companyId;
                        }else{
                        this.editForm={
                            appId:'',
                            appSecret:'',
                            templateId:''
                        };
                        this.companyId=res.data.companyId;   
                        // this.initQCode='/api/auth/wechat/bind_alarm?appId='+res.data.appId+'&companyId='+this.companyId;
                        // console.log(this.initQCode)
                            this.mptype=1
                        }
                    }
       
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该用户吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    let para = {
                        id: row.id
                    };
                    
                    user_weixin_delete(para).then((res) => {
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        } else {
                            this.$message({
                                message: res.message,
                                type: 'warning'
                            });
                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    englishName: '',
                    parentId: '',
                    path: '',
                    icon:''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                          
                            queryput(para).then((res) => {
                                if (res.code == 0) {
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: 'warning'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            if(this.mptype==0){
                                company_weixin_put(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                
                                if (res.code == 0) {
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: 'warning'
                                    });
                                }
                                
                            });
                            }else{
                                company_weixin_post(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                if (res.code == 0) {
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: 'warning'
                                    });
                                }
                            });
                            }
                          
                        });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        ids: ids
                    };
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getUsers();
        }
    }
</script>

<style scoped>

</style>